﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_Article.Business
{
    public class DBArticlePropertyValue
    {
        readonly LinqArticle db = new LinqArticle();

        public ud_ArticlePropertyValue GetOne(Guid guid)
        {
            var result = from p in db.ud_ArticlePropertyValues
                         where p.Guid == guid
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_ArticlePropertyValue> GetAll(int moduleID)
        {
            var result = from p in db.ud_ArticlePropertyValues
                         where p.ModuleID == moduleID
                         select p;
            return result.ToList();
        }

        public bool Delete(Guid guid)
        {
            bool flag = false;
            ud_ArticlePropertyValue item = GetOne(guid);
            if (item != null)
            {
                db.ud_ArticlePropertyValues.DeleteOnSubmit(item);
            }
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Create(int moduleID, Guid propertyGuid, string propertyValue, string unitValue)
        {
            bool flag = false;

            ud_ArticlePropertyValue item = new ud_ArticlePropertyValue
            {
                ModuleID = moduleID,
                Guid = Guid.NewGuid(),
                PropertyGuid = propertyGuid,
                PropertyValue = propertyValue,
                UnitValue = unitValue
            };
            db.ud_ArticlePropertyValues.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid guid, Guid propertyGuid, string propertyValue, string unitValue)
        {
            bool flag = false;
            ud_ArticlePropertyValue item = GetOne(guid);
            if (item != null)
            {
                item.PropertyGuid = propertyGuid;
                item.PropertyValue = propertyValue;
                item.UnitValue = unitValue;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            List<ud_ArticlePropertyValue> listItem = GetAll(moduleID);
            db.ud_ArticlePropertyValues.DeleteAllOnSubmit(listItem);
            db.SubmitChanges();
        }
    }
}
